package com.house.java.biz;

import java.util.ArrayList;
import java.util.List;

import com.house.java.dao.DaoFactory;
import com.house.java.dao.HouseSurroundDAO;
import com.house.java.dao.NewHouseDAO;
import com.house.java.dao.NewsDAO;
import com.house.java.dao.PriceHistoryDAO;
import com.house.java.dao.impl.HouseSurroundDaoImpl;
import com.house.java.dao.impl.NewHouseDaoImpl;
import com.house.java.dao.impl.NewsDaoImpl;
import com.house.java.dao.impl.PriceHistoryDaoImpl;
import com.house.java.model.HistoryPrice;
import com.house.java.model.HouseSurround;
import com.house.java.model.NewHouse;
import com.house.java.model.News;
import com.house.java.model.Paging;

public class NewsBusiness {
	
	private NewsDAO dao = null;
	
	public NewsBusiness(){
		dao = (NewsDaoImpl)DaoFactory.getDaoImpl(NewsDAO.class);
	}

	public List<News> queryNews(Integer locationCityCode,Integer locationProvinceCode,
			Integer locationCountryCode,Integer currentPage,Integer pageSize){
		List<News> list = new ArrayList<News>();
		Paging paging = new Paging();
		if(currentPage!=null){
			paging.setCurrentPage(currentPage);
			if(null != pageSize){
				paging.setPageSize(pageSize);
			}else {
				paging.setPageSize(10);
			}
		}else {
			paging.setCurrentPage(1);
			paging.setPageSize(10);
		}
		list = dao.queryNewsByCategory(locationCountryCode, locationProvinceCode, locationCityCode, paging);
		return list;
	}
	
	
	public News queryNewsById(Long id){
		List<News> list = new ArrayList<News>();
		list = dao.queryById(id);
		if(null != list && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	/**
	 * 添加news对象
	 * @param house  
	 * @return
	 */
	public boolean addNews(News news){
		if(null != news.getId()){
			return update(news);
		}else {
			dao.addNews(news);
			if(null != news.getId()){
				return true;
			}
			return false;
		}
	}
	
	
	public boolean update(News news){
		News newsDB = queryNewsById(news.getId());
		if(null!=newsDB){
			int deleteResult = dao.delete(news.getId());
			if(deleteResult > 0){
				news.setId(null);
				addNews(news);
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	
	
	public boolean delete(Long id){
		if(null != id){
			int result = dao.delete(id);
			if(result > 0){
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}

}
